package kevin.ecstorm.pdd.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ©Copyright ©1968-2020 Midea Group,IT
 * FileName: ConfigBean
 * Author: kevin
 * Date: 2020年04月12日 00:14:09
 * Description: ConfigBean服务接口
 */
@Configuration
public class ConfigBean {
    @Autowired
    private PddAppConfig pddAppConfig;
    private String accessToken = "365969f7681742e59e6de423e87cbe58240b6ee9";
    private String refreshToken = "32b3f8c6faba47bfae83b94bb0a6961054e9587c";

    @Bean
    public PddAppAuth getPddAppAuth() {
        PddAppAuth auth = new PddAppAuth();
        auth.setClientId(pddAppConfig.getClientId());
        auth.setClientSecret(pddAppConfig.getClientSecret());
        auth.setAccessToken(accessToken);
        auth.setRefreshToken(refreshToken);
        return auth;
    }

    //全局配置跨域
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        HandlerInterceptorAdapter hia = new HandlerInterceptorAdapter() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "x_requested_with,x-requested-with,Authorization,Content-Type,token");
                response.setHeader("Access-Control-Allow-Credentials", "true");
                return true;
            }
        };

        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(hia).addPathPatterns("/**");
            }
        };
    }
}
